/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.servicesImpl;

import cz.muni.fi.heathrow.dao.UserDAO;
import cz.muni.fi.heathrow.dto.DTOAssembler;
import cz.muni.fi.heathrow.entities.User;
import cz.muni.fi.heathrowapi.UserService;
import cz.muni.fi.heathrowapi.dto.UserDTO;
import java.util.List;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author nekdo
 */
public class UserServiceImpl implements UserService
{

    private UserDAO userDao;

    public void setUserDao(UserDAO userDao)
    {
        this.userDao = userDao;
    }
    private PasswordEncoder passwordEncoder;

    public void setPasswordEncoder(PasswordEncoder passwordEncoder)
    {
        this.passwordEncoder = passwordEncoder;
    }

    @Transactional(readOnly=true)
    @Override
    public UserDTO get(String username)
    {
        if (username == null)
            throw new IllegalArgumentException("username is null");

        return DTOAssembler.CreateUserDTO(userDao.get(username));
    }

    @Transactional(readOnly=true)
    @Override
    public UserDTO get(Long id)
    {
        if (id == null)
            throw new IllegalArgumentException("id is null");

        return DTOAssembler.CreateUserDTO(userDao.get(id));
    }

    @Transactional
    @Override
    public void create(UserDTO user)
    {
        if (user == null)
            throw new IllegalArgumentException("user is null");

        User user1 = new User();
        DTOAssembler.UpdateUserDomainObject(user, user1, true);
        userDao.create(user1);
        DTOAssembler.UpdateUserDTO(user, user1);
    }

    @Transactional
    @Override
    public void remove(UserDTO user)
    {
        if (user == null)
            throw new IllegalArgumentException("user is null");

        userDao.remove(userDao.get(user.getId()));
    }

    @Transactional
    @Override
    public void update(UserDTO user)
    {
        if (user == null)
            throw new IllegalArgumentException("user is null");

        User us = userDao.get(user.getId());
        DTOAssembler.UpdateUserDomainObject(user, us, false);

        userDao.update(us);
    }

    @Transactional(readOnly=true)
    @Override
    public List<UserDTO> findAll()
    {
        List<User> users;
        List<UserDTO> dtoUsers;

        users = userDao.findAll();
        dtoUsers = DTOAssembler.createUserDTOList(users);

        return dtoUsers;
    }

    @Transactional
    @Override
    public void updatePassword(UserDTO user, String password)
    {
        User u = userDao.get(user.getId());        
        String hash = getPasswordHash(u.getUsername(), password);
        u.setPasswordHash(hash);
        userDao.update(u);
    }

    @Transactional(readOnly=true)
    @Override
    public boolean checkPassword(UserDTO user, String password)
    {
        User u = userDao.get(user.getId());
        String hash = getPasswordHash(u.getUsername(), password);
        return hash.equals(u.getPasswordHash());
    }

    private String getPasswordHash(String username, String password)
    {
        return passwordEncoder.encodePassword(password, username);
    }
}
